<!-- 今日加速流量趋势 -->
<template>
  <div class="wrap">
    <span class="title">今日加速流量趋势</span>
    <div
      ref="linechart"
      :class="className"
      :style="{ height: height, width: width }"
      v-if="chartData"
    />
  </div>
</template>

<script>
import * as echarts from "echarts";
import { formatBytes } from "@/utils/index.js";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "316px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      color:''
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val);
      }
    }
  },
  mounted() {
    this.color = localStorage.getItem("isNightMode") || "night";
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.linechart, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ TransTime, UpByte, DownByte } = {}) {
      this.chart.setOption({
        // dataZoom: {
        //   start: 0,
        //   end: 100
        // },
        grid: {
          left: 20,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          formatter: function (params) {
            return `${params[1].name}<br/>${params[1].marker}上行：${(
              params[1].value / 1024
            ).toFixed(2)}KB/S<br/>${params[0].marker}下载：${(
              params[0].value / 1024
            ).toFixed(2)}KB/S`;
          },
          padding: [5, 10]
        },
        yAxis: {
          axisLabel: {
            formatter: function (value) {
              return (value / 1024).toFixed(2) + " KB";
            }
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: "#666", // 修改网格线颜色为浅灰色
              type: "dashed", // 添加虚线效果
              width: 1 // 线条宽度
            }
          }
        },
        legend: {
          data: ["上行", "下载", "单位(KB/S)"],
          selectedMode: false,
          textStyle: {
            color: this.color === "night" ? "#fff" : "#000" // 深色模式用白色，浅色模式用黑色
          }
        },
        xAxis: {
          data: TransTime,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        series: [
          {
            name: "下载",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2
                },
                areaStyle: {
                  // color: 'rgba(24, 144, 255, .2)'
                }
              }
            },
            symbol: "none",
            data: DownByte,
            animationDuration: 2800,
            animationEasing: "quadraticOut",
            areaStyle: {}
          },
          {
            name: "上行",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2
                }
              }
            },
            symbol: "none",
            smooth: true,
            type: "line",
            data: UpByte,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
            areaStyle: {}
          },
          {
            name: "单位(KB/S)",
            smooth: true,
            type: "bar"
          }
        ]
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.wrap {
  height: calc(50vh - 50px) !important;
}
</style>
